﻿using LunchLearn.WebApi.Hubs;
using LunchLearn.WebApi.Infrastructure;
using LunchLearn.WebApi.Infrastructure.Notifications;
using LunchLearn.WebApi.Infrastructure.TrackingServices;
using LunchLearn.WebApi.Repository;
using LunchLearn.WebApi.Unity;
using Microsoft.AspNet.SignalR;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;

namespace LunchLearn.WebApi.App_Start
{
    public static class UnityConfig
    {
        public static void Register(HttpConfiguration config, IUnityContainer container)
        {
            config.DependencyResolver = new UnityDependencyResolver(container);
            container.RegisterInstance<IHttpControllerActivator>(new UnityHttpControllerActivator(container));
            foreach (var type in
                Assembly.GetExecutingAssembly().GetExportedTypes().
                            Where(x => x.GetInterface(typeof(IHttpController).Name) != null))
            {
                container.RegisterType(type);
            }
        }

        public static void RegisterDependencies(UnityContainer _container)
        {
            _container
                .RegisterType<OrderHub>(new ContainerControlledLifetimeManager())
                .RegisterType<IContextProvider, ContextProvider>(new HierarchicalLifetimeManager())
                .RegisterType<INotificationBridge, NotificationBridge>(new ContainerControlledLifetimeManager())
                .RegisterType<IChangeTrackingService, ChangeTrackingService>()
                .RegisterType<OrderTrackingService>()
                ;
        }
    }
}